package guoanmaker.operator.business.service;

import guoanmaker.operator.business.model.OperatorActivityType;
import guoanmaker.operator.business.repository.OperatorActivityTypeRepository;
import guoanmaker.operator.helper.common.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  
 *
 * @ClassName: OperatorActivityTypeService 
 * @Description: OperatorActivityTypeService 
 * @author: huZhaoyu 
 * @date 2017/6/28 下午2:56 
 */
@Service
public class OperatorActivityTypeService implements OperatorActivityTypeServiceInterface {
    private final OperatorActivityTypeRepository operatorActivityTypeRepository;

    @Override
    public Integer countAllById() {
        return operatorActivityTypeRepository.countAllByIdIsNotNull();
    }

    @Override
    public Page<OperatorActivityType> getActivityTypeByPageOrderByWeight(Integer page, Integer size) {
        return operatorActivityTypeRepository.findAll(new PageRequest(page, size, new Sort(Sort.Direction.DESC, "weight")));
    }

    @Autowired
    public OperatorActivityTypeService(OperatorActivityTypeRepository operatorActivityTypeRepository) {
        this.operatorActivityTypeRepository = operatorActivityTypeRepository;
    }

    @Override
    public OperatorActivityType saveActivityType(OperatorActivityType operatorActivityType) {
	   if(operatorActivityType.getId()==null || operatorActivityType.getId()==""){
		   List<OperatorActivityType> list=operatorActivityTypeRepository.findAll();
		    for(OperatorActivityType typelist: list){
		    	if(typelist.getTypeName().equals(operatorActivityType.getTypeName())==true){
		    		return null;
		    	}
		    }
	   }
        return operatorActivityTypeRepository.save(operatorActivityType);
    }

    @Override
    public List<OperatorActivityType> getAllActivityType() {
        return operatorActivityTypeRepository.findAll();
    }

    @Override
    public Page<OperatorActivityType> getAllActivityTypeByPage(Integer page, Integer size) {
        return operatorActivityTypeRepository.findAll(new PageRequest(page, size));
    }

    @Override
    public void deleteActivityTypeById(String id) {
        operatorActivityTypeRepository.delete(id);
    }

    @Override
    public OperatorActivityType getActivityTypeById(String id) {
        return operatorActivityTypeRepository.findOne(id);
    }
}
